\documentclass[a4paper,12pt]{article}

\usepackage[margin=1.5cm]{geometry}
\usepackage{listings}
\usepackage{enumitem}

\title{Misc about Relocation}
\author{Chen Rushan\\chenrsster@gmail.com}
\date{2009.10.25 21:09}

\setlength{\parskip}{1.2ex}
\setlength{\parindent}{0pt}

\lstset{
    frame=single, framesep=0pt, framexleftmargin=-1pt, framextopmargin=3pt,
    framexbottommargin=3pt, aboveskip=0.5cm, belowskip=0.2cm, columns=flexible,
    basicstyle=\ttfamily
}

\begin{document}

\maketitle

Before we start the discussion of relocation, let's first look at two useful
commands:

\begin{lstlisting}[escapechar=\%]
 objdump -d <%{\em binary}%>
 readelf -r <%{\em binary}%>
\end{lstlisting}

The first command display assembler contents of executable sections, and the
second one display the relocation info.

Now suppose there's a program consisting of two files:

{\em obj.c:}

\begin{lstlisting}
 int global = 0;
 
 void print(void)
 {
         global = 1;
 }
\end{lstlisting}

{\em main.c:}

\begin{lstlisting}
 int main(int argc, char **argv)
 {
         return 0;
 }
\end{lstlisting}

Now compile {\em obj.c} into object file:

\begin{lstlisting}
 gcc -c obj.c
\end{lstlisting}

\verb/objdump -d obj.o/ gives the following assemble code:

\begin{lstlisting}
 00000000 <print>:
    0:   55                      push   %ebp
    1:   89 e5                   mov    %esp,%ebp
    3:   c7 05 00 00 00 00 01    movl   $0x1,0x0
    a:   00 00 00 
    d:   5d                      pop    %ebp
    e:   c3                      ret    
\end{lstlisting}

The leftmost column in the above output denotes the offset of current line, the
middle block which consists of multiple columns of hex numbers denotes the
opcode of the instruction at the same line and its operands, and the rightmost
block lists the human-readable instructions.

We can easily find that the line \verb/global = 1/ in {\em obj.c} corresponds to
the instruction \verb/movl $0x1, 0x0/, so why 1 is moved to an address of 0. In
fact, this 0 is just a place holder, the complier currently doesn't determine
the address of {\em global}, and it's the job of linker to fill it in. So how
the linker knows which span of bytes in a object file needs to be replaced, well
it's basically to do with the {\em relocation} info stored in the object file.

As mentioned above, by running \verb/readelf -r obj.o/, we get:

\begin{lstlisting}
 relocation section '.rel.text' at offset 0x2dc contains 1 entries:
  Offset     Info    Type            Sym.Value  Sym. Name
 00000005  00000701 R_386_32          00000000   global
\end{lstlisting}

\textbf{R\_386\_32} here means "relocate 32-bit absolute address", the overall
output tells linker to replace {\em offset 00000005} with a 32-bit absolute
address. (There are many other relocation types like \textbf{R\_386\_PC32},
which relocates PC relative address)

Appendix shows a typical overall structure of a object file (.o).

Now link {\em obj.o} against {\em main.o}, and running \verb/objdump -d/ on
result file, among the massive output, there's a section for {\em print} looks
like this:

\begin{lstlisting}
 08048380 <print>:
  8048380:    55                      push   %ebp
  8048381:    89 e5                   mov    %esp,%ebp
  8048383:    c7 05 60 95 04 08 01    movl   $0x1,0x8049560
  804838a:    00 00 00 
  804838d:    5d                      pop    %ebp
  804838e:    c3                      ret
  804838f:    90                      nop
\end{lstlisting}

Now we find that the original \textbf{movl \$0x1,0x0} now becomes \textbf{movl
\$0x1,0x8049560}. {\em 0x8049560} here is the final address of {\em global}.

Now another question arises about how does the linker knows the address of a
global variable without even reading the executable into memory. The answer to
this question involves the knowledge of {\em virtual memory}.

%TODO

\begin{lstlisting}[escapechar=\%]
%\begin{center} TODO \end{center}%
\end{lstlisting}

\appendix
\section*{Appendix}

The following image shows what a typical structure of a object file (.o) looks
like, it is grabbed from elsewhere. In fact, the structure of object file on my
system differs from this one, you can find it by running:

\begin{lstlisting}[escapechar=\%]
 readelf -S <%{\em object file}%>
\end{lstlisting}

{\em readelf} is such a command that used to read sections of an object file,
and display in a human-readable form.

%TODO add the page here

\end{document}

